<?php
namespace frontend\controllers;

use Yii;
use zhongkefu\zhongkefu;
use yii\web\Controller;
use yii\base\ErrorException;
use yii\web\NotFoundHttpException;
use Omnipay\Omnipay;
use yii\filters\AccessControl;

class AlipayController extends Controller
{

    public $enableCsrfValidation = false;

    public function actionIndex(){
        $gateway = Omnipay::create('Alipay_LegacyExpress');
        $gateway->setSellerEmail('419878271@qq.com');
        $gateway->setPartner('2088421525829062');
        $gateway->setKey('4rqnn4hywc8xobzcvdpbaigiesy9478t'); 
        // $gateway->setPrivateKey('the_rsa_sign_key');
        // $gateway->setAlipayPublicKey('the_alipay_public_key');
        $gateway->setReturnUrl('http://fsxh.zhongkefu.com.cn/alipay/return');
        $gateway->setNotifyUrl('http://fsxh.zhongkefu.com.cn/alipay/notify');


        $request = $gateway->purchase([
            'out_trade_no' => date('YmdHis').mt_rand(1000,9999),
            'subject'      => 'test',    //调取数据库字段
            'total_fee'    => '0.01',    //调取数据库字段
        ]);

        /**
        * @var LegacyExpressPurchaseResponse $response
        */
        $response = $request->send();

        $redirectUrl = $response->getRedirectUrl();
        //or 
        $response->redirect();

    }
    public function actionReturn(){
        $gateway = Omnipay::create('Alipay_LegacyExpress');
        $gateway->setSellerEmail('419878271@qq.com');
        $gateway->setPartner('2088421525829062');
        $gateway->setKey('4rqnn4hywc8xobzcvdpbaigiesy9478t'); 
        $options = array_merge($_POST, $_GET);
        //Don't use $_REQUEST for may contain $_COOKIE

        $request = $gateway->completePurchase($options);
        /**
        * @var AopTradeAppPayResponse $response
        */
        $response = $request->send();

        try {
            $response = $request->send();

            if($response->isPaid()){
                /**
                * Payment is successful 更新数据库支付状态字段
                */
                die('success'); //The notify response should be 'success' only
            }else{
                /**
                * Payment is not successful
                */
                die('fail'); //The notify response
            }
        } catch (Exception $e) {
            /**
            * Payment is not successful
            */
            die('fail'); //The notify response
        }
    }
    public function actionNotify(){
        $gateway = Omnipay::create('Alipay_LegacyExpress');        
        $gateway->setSellerEmail('419878271@qq.com');
        $gateway->setPartner('2088421525829062');
        $gateway->setKey('4rqnn4hywc8xobzcvdpbaigiesy9478t'); 
        $options = array_merge($_POST, $_GET);
        //Don't use $_REQUEST for may contain $_COOKIE

        $request = $gateway->completePurchase($options);
        /**
        * @var AopTradeAppPayResponse $response
        */
        $response = $request->send();

        try {
            $response = $request->send();

            if($response->isPaid()){
                /**
                * Payment is successful 更新数据库支付状态字段
                */
                die('success'); //The notify response should be 'success' only
            }else{
                /**
                * Payment is not successful
                */
                die('fail'); //The notify response
            }
        } catch (Exception $e) {
            /**
            * Payment is not successful
            */
            die('fail'); //The notify response
        }


    }

}